package com.android.qjpmc.application_module.viewModel;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.android.qjpmc.application_module.contract.RtCurveDataContract;
import com.android.qjpmc.application_module.home.model.StationDataModel;
import com.android.qjpmc.application_module.model.RtDataModel;
import com.android.qjpmc.bean.GroupInfoPointDataBean;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.widget.mvvm.factory.CreateModel;
import com.android.qjpmc.widget.retrofithelper.rxschedulers.RxSchedulersHelper;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.ProgressObserver;
import com.android.qjpmc.widget.utils.DateUtils;
import com.android.qjpmc.widget.utils.TimeUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

@CreateModel(RtDataModel.class)
public class RtCurveDataViewModel extends RtCurveDataContract.ViewModel {
    @Override
    public void getPointListOfGroup(PwrDeviceBean data) {
        mModel.getPointListOfGroup(data.groupId, 1).subscribe(new ProgressObserver<List<PointParameterBean>>(false, this) {
            @Override
            public void _onNext(List<PointParameterBean> groupPointList) {
                //  getPointDataOfGroup(stationId, groupId,groupPointList);
                data.pointList = groupPointList;
                mView.returnGroupPointList(data);
            }
        });

    }

    long requestDataTime = 0;

    public void getPointDataOfStation(boolean isRefresh, String stationId, List<PointParameterBean> pointList) {
        //刷新的话 每隔三秒记一下时间
        if (isRefresh) {
            requestDataTime += 3000;
        } else {
            requestDataTime = System.currentTimeMillis();
        }
        //请求数据的pointCode List
        List<String> pointCodeList = new ArrayList<>();
        for (PointParameterBean groupPointListBean : pointList) {
            String pointCode = StationDataModel.buildPointCode(groupPointListBean);
            //服务器端返回的数据是用pointCode来作为key的 所以需要存下来
            groupPointListBean.pointCode = pointCode;
            pointCodeList.add(pointCode);
        }
        mModel.getPointDataOfPointCode(stationId, pointCodeList).map(new Function<Object, List<PointParameterBean>>() {
                    @Override
                    public List<PointParameterBean> apply(Object o) throws Exception {
                        String s = JSONObject.toJSONString(o);
                        Logger.d("getPointDataOfStation" + s);
                        JSONObject jsonObject = JSONObject.parseObject(s);
                        Logger.d("getPointDataOfStation22" + Thread.currentThread().getName());
                        //构建解析数据，服务器端返回的数据是用pointCode来作为key的 用jsonObject 通过key取到值
                        for (PointParameterBean groupPointListBean : pointList) {
                            JSONObject pointData = jsonObject.getJSONObject(groupPointListBean.pointCode);
                            //解析到Key对应的值
                            GroupInfoPointDataBean groupInfoPointDataBean = pointData.toJavaObject(GroupInfoPointDataBean.class);
                            groupInfoPointDataBean.requestDataTime = requestDataTime;
                            groupPointListBean.pointData = groupInfoPointDataBean;
                        }
                        return pointList;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new ProgressObserver<List<PointParameterBean>>(!isRefresh, this) {
                    @Override
                    public void _onNext(List<PointParameterBean> pointDataList) {
                        if (mView != null)
                            mView.returnGroupPointDataList(pointDataList, isRefresh);
                    }
                });


    }
}
